package com.adityayadav.peekaboo;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

import com.adityayadav.parallelism.core.ThreadPool;

public class MainJFrame extends JFrame {

	private JTable table = null;
	private DefaultTableModel model = null;
	private ArrayList<Socket> sockets = new ArrayList<Socket>();
	
	public MainJFrame(String[] args) throws Exception{
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(true);
		this.setTitle("Peekaboo ver 1.0 [http://adityayadav.com]");
		this.setMinimumSize(new Dimension(640,480));
		this.setSize(new Dimension(800,600));
		this.setLayout(new BorderLayout());

		
		
		
		//start the proxy server thread
		final ThreadPool pool = new ThreadPool(50);// number of threads
		
		final ServerSocket ss = new ServerSocket(8888);// # port
		Thread ssThread = new Thread(){
			public void run(){
				while(true){
					try {
						Socket incoming = ss.accept();
						SocketAcceptorTask task = new SocketAcceptorTask(MainJFrame.this, pool, incoming);
						pool.addTask(task);
					} catch (Exception e){}
				}
			}
		};
		ssThread.start();
		
		/*// Untested SSL Proxy code. Cannot Peek into it so not of much use
		SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
		final SSLServerSocket sslss = (SSLServerSocket) sslserversocketfactory.createServerSocket(8889);
		Thread sslssThread = new Thread(){
			public void run(){
				while(true){
					try {
						SSLSocket incoming = (SSLSocket) sslss.accept();
						SSLSocketAcceptorTask task = new SSLSocketAcceptorTask(pool, incoming);
						pool.addTask(task);
					} catch (Exception e){}
				}
			}
		};
		sslssThread.start();
		*/
		
		model = new DefaultTableModel(){
			@Override
			public boolean isCellEditable(int row, int col) {
		    	return false;
		    }
		};
		model.addColumn("URL");
		table = new JTable(model);
		table.setRowSelectionAllowed(true);
		table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.addMouseListener(new MouseAdapter(){
		     public void mouseClicked(MouseEvent e){
		         if (e.getClickCount() == 2){
	        		 int row = table.getSelectedRow();
	        		 try {
	        			 String url = MainJFrame.this.model.getValueAt(row,0).toString();
	        			 Socket socket = sockets.get(row);
	        			 Peeker peeker = new Peeker(url, socket);
	        			 
	        		 } catch (Exception f){}
		         }
		     }
		});
		JScrollPane pane = new JScrollPane(table);
	    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		this.getContentPane().add(table.getTableHeader(), BorderLayout.PAGE_START);
		this.getContentPane().add(pane, BorderLayout.CENTER);
		// create the UI components
		
		this.setVisible(true);
	}
		
	public void addSocket(String url, Socket socket){
	    	//ArrayList<Object[]> buffer = new ArrayList<Object[]>();
	    	//for (int i=0; i<data.length; i++){
	    	//	buffer.add(data[i]);
	    	//}
	    	Object[] part = new Object[]{url};
	    	sockets.add(socket);
	    	model.addRow(part);	    	
	    	//buffer.add(0,part);
	    	//part = buffer.toArray(new Object[0]);
	    	//MainJFrame.this.table.getRowModel().setModel(this);
	}
	/*
	class MyTableModel extends AbstractTableModel {
	    private String[] columnNames = new String[]{"URL"};
	    private Object[][] data = {
	    	    {"Kathy", "Smith"},
	    	       {"John", "Doe"},
	    	       {"Sue", "Black"},
	    	       {"Jane", "White"},
	    	       {"Joe", "Brown"}
	    	   };

	    public int getColumnCount() {
	        return columnNames.length;
	    }

	    public int getRowCount() {
	        return data.length;
	    }

	    public String getColumnName(int col) {
	        return columnNames[col];
	    }

	    public Object getValueAt(int row, int col) {
	        return data[row][col];
	    }

	    public Class getColumnClass(int c) {
	        return getValueAt(0, c).getClass();
	    }

	    public boolean isCellEditable(int row, int col) {
	    	return false;
	    }

	    
	}*/
}
